package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.vo.CatalogVO;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("strategy")
@Controller
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") StrategyQuery qo){
        Page<Strategy> page = strategyService.queryPageList(qo);
        model.addAttribute("page",page);

        return "strategy/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        if(StringUtils.hasLength(strategy.getId())){
            strategyService.update(strategy);
        }else{
            strategyService.save(strategy);
        }
        return JsonResult.success();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        strategyService.delete(id);
        return JsonResult.success();
    }

    @RequestMapping("input")
    public String input(String id,Model model){
        if(StringUtils.hasLength(id)){
            Strategy strategy = strategyService.get(id);
            model.addAttribute(strategy);
        }
        List<CatalogVO> catalogs = strategyCatalogService.queryStrategyCatalogList();
        model.addAttribute("catalogs",catalogs);
        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes",themes);
        return "strategy/input";
    }


    @RequestMapping("changeState")
    @ResponseBody
    public Object changeState(int state,String id){
        strategyService.changeState(state,id);
        return JsonResult.success();
    }
}
